<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <script type="text/javascript" src="./script/rem.js"></script>
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta content="yes" name="apple-mobile-web-app-capable">
    <meta content="yes" name="apple-touch-fullscreen">
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="./css/font-awesome.css" />
    <link rel="stylesheet" type="text/css" href="./css/style.css" />
    <script type="text/javascript" src="./script/jquery.min.js"></script>
    <script type="text/javascript" src="./script/aui-slide.js"></script>
    <script type="text/javascript" src="./script/jquery.min.js"></script>
    <script type="text/javascript" src="./script/doT.min.js"></script>
    <script type="text/javascript" src="./script/base.js"></script>
    <script type="text/javascript" src="./script/api.js"></script></head>
    <script type="text/javascript" src="./script/aui-tab.js" ></script>
    <script type="text/javascript" src="./script/jquery-3.2.0.js"></script>
</head>
<body>

    <header class="head" style="padding-top:0.4rem;">
        <div class="head-l" tapmode onclick="closeWin()"><i class="fa fa-angle-left"></i></div>
        <h3>填写标题</h3>
        <div class="head-r" onclick="save();">保存</div>
    </header>
    <div class="head-d" style="padding-top:0.4rem;"></div>
    <div class="list-text-r mb20">
        <ul class="find-video">
            <li>
                <label>标题:</label>
                <input type="text" placeholder="请输入标题" id="title">
            </li>
            <li onclick="chooseIMG();">
                <label>上传封面:</label>
                <span id="uploadPic">待上传</span>
                <i class="fa fa-angle-right"></i>
            </li>
            <li onclick="chooseVideo();">
                <label>上传视频:</label>
                <span id="uploadVideo">待上传</span>
                <i class="fa fa-angle-right"></i>
            </li>
        </ul>
    </div>
</body>
<script type="text/javascript" src="./script/api.js"></script>
<script type="text/javascript" src="./script/index.js"></script>
<script type="text/javascript">

    apiready = function(){
        filePath = '';
        videoPath= '';
        // reloadListener(); 
    }

    function closeWin(){
        api.closeWin({
        });
    }

    // 选择图片路径
    function chooseIMG(){
        api.actionSheet({
            // title: '底部弹出框测试',
            cancelTitle: '取消',
            // destructiveTitle: '红色警告按钮',
            buttons: ['拍照', '从手机上传照片']
        }, function(ret, err) {
            var index = ret.buttonIndex;
            if (index == 1) {
                //拍照
                takePicture('camera');
            } else if (index == 2) {
                //从手机上传
                takePicture('library');
            }
        });
    }

    // 选择图片
    function takePicture(sourceType){
        api.getPicture({
            sourceType: sourceType,
            encodingType: 'jpg',
            mediaValue: 'pic',
            destinationType: 'url',
            allowEdit: true,
            quality: 50,
            // targetWidth: 750,
            // targetHeight: 400,
            saveToPhotoAlbum: false,
        }, function(ret, err) {
            if (ret) {
               // alert(JSON.stringify(ret));
               filePath = ret.data;
               $('#uploadPic').html('已上传');
           } else {
            // alert(JSON.stringify(err));
        }
    });
    }

    // 选择视频路径
    function chooseVideo(){
        api.actionSheet({
            // title: '底部弹出框测试',
            cancelTitle: '取消',
            // destructiveTitle: '红色警告按钮',
            buttons: ['拍摄', '从手机上传视频']
        }, function(ret, err) {
            var index = ret.buttonIndex;
            if (index == 1) {
                //拍照
                takeVideo('camera');
            } else if (index == 2) {
                //从手机上传
                takeVideo('library');
            }
        });
    }

    // 选择视频
    function takeVideo(sourceType){

        api.getPicture({
            sourceType: sourceType,
            encodingType: 'jpg',
            mediaValue: 'video',
            destinationType: 'url',
            allowEdit: true,
            quality: 50,
            targetWidth: 100,
            targetHeight: 100,
            saveToPhotoAlbum: false,
        }, function(ret, err) {
            if (ret) {
               // alert(JSON.stringify(ret));
               api.hideProgress();
               videoPath = ret.data;
               $('#uploadVideo').html('已上传');
           } else {
            // alert(JSON.stringify(err));
        }
    });
    }

    // 保存
    function save() {

        var title = $api.byId('title').value;
        var token = $api.getStorage('token');

        if (!title) {
            api.toast({
                msg: '请添加标题!',
                duration: 2000,
                location: 'bottom'
            });
            return false;
        }

        if (!filePath) {
            api.toast({
                msg: '请添加图片!',
                duration: 2000,
                location: 'bottom'
            });
            return false;
        }

        if (!videoPath) {
            api.toast({
                msg: '请添加视频!',
                duration: 2000,
                location: 'bottom'
            });
            return false;
        }

        api.showProgress({
            style: 'default',
            animationType: 'fade',
            title: '正在保存...',
            modal: false
        });

        api.ajax({
            url: webAddress + '/api/education/create',
            method: 'post',
            data: {
                values: {
                    title: title,
                },
                files: { 
                    photo: filePath,
                    video: videoPath,
                }
            },
            headers: {
                Authorization: 'Bearer ' + token,
                Accept: 'application/json',
            },
        },function(ret, err){
            if (ret) {
                // alert( JSON.stringify( ret ) );
                api.hideProgress();
                if (ret.status_code == 200) {
                    api.toast({
                        msg: ret.message,
                        duration: 2000,
                        location: 'bottom',
                        global: true,
                    });
                }
                api.sendEvent({
                  name: 'reload'
              });
                api.closeWin();
            } else {
                // alert( JSON.stringify( err ) );
                api.hideProgress();
                if (err.body.status_code == 401) {
                    api.openWin({
                        name: 'login',
                        url: './login.html',
                        pageParam: {
                            name: 'find_details_video'
                        }
                    });
                }else{
                    api.toast({
                        msg: err.body.message,
                        duration: 2000,
                        location: 'bottom'
                    });
                    return false;
                }
            }
        });
    }

  //   // 监听事件 (页面刷新)
  //   function reloadListener(){
  //     api.addEventListener({
  //       name: 'reload',
  //   }, function(ret, err){

  //   });
  // }
</script>
</html>